home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / ecofflink.c < prev    next >
C/C++ Source or Header  |  1994-06-21  |  46KB  |  1,617 lines

  1. /* Routines to link ECOFF debugging information.
  2.    Copyright 1993 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor, Cygnus Support, <ian@cygnus.com>.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "bfdlink.h"
  24. #include "libbfd.h"
  25. #include "obstack.h"
  26. #include "coff/internal.h"
  27. #include "coff/sym.h"
  28. #include "coff/symconst.h"
  29. #include "coff/ecoff.h"
  30.  
  31. static boolean ecoff_add_bytes PARAMS ((char **buf, char **bufend,
  32.                     size_t need));
  33. static struct bfd_hash_entry *string_hash_newfunc
  34.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
  35.        const char *));
  36. static void ecoff_align_debug PARAMS ((bfd *abfd,
  37.                        struct ecoff_debug_info *debug,
  38.                        const struct ecoff_debug_swap *swap));
  39. static boolean ecoff_write_symhdr PARAMS ((bfd *, struct ecoff_debug_info *,
  40.                        const struct ecoff_debug_swap *,
  41.                        file_ptr where));
  42.  
  43. /* Obstack allocation and deallocation routines.  */
  44. #define obstack_chunk_alloc malloc
  45. #define obstack_chunk_free free
  46.  
  47. /* The minimum amount of data to allocate.  */
  48. #define ALLOC_SIZE (4064)
  49.  
  50. /* Add bytes to a buffer.  Return success.  */
  51.  
  52. static boolean
  53. ecoff_add_bytes (buf, bufend, need)
  54.      char **buf;
  55.      char **bufend;
  56.      size_t need;
  57. {
  58.   size_t have;
  59.   size_t want;
  60.   char *newbuf;
  61.  
  62.   have = *bufend - *buf;
  63.   if (have > need)
  64.     want = ALLOC_SIZE;
  65.   else
  66.     {
  67.       want = need - have;
  68.       if (want < ALLOC_SIZE)
  69.     want = ALLOC_SIZE;
  70.     }
  71.   if (*buf == NULL)
  72.     newbuf = (char *) malloc (have + want);
  73.   else
  74.     newbuf = (char *) realloc (*buf, have + want);
  75.   if (newbuf == NULL)
  76.     {
  77.       bfd_set_error (bfd_error_no_memory);
  78.       return false;
  79.     }
  80.   *buf = newbuf;
  81.   *bufend = *buf + have + want;
  82.   return true;
  83. }
  84.  
  85. /* We keep a hash table which maps strings to numbers.  We use it to
  86.    map FDR names to indices in the output file, and to map local
  87.    strings when combining stabs debugging information.  */
  88.  
  89. struct string_hash_entry
  90. {
  91.   struct bfd_hash_entry root;
  92.   /* FDR index or string table offset.  */
  93.   long val;
  94.   /* Next entry in string table.  */
  95.   struct string_hash_entry *next;
  96. };
  97.  
  98. struct string_hash_table
  99. {
  100.   struct bfd_hash_table table;
  101. };
  102.  
  103. /* Routine to create an entry in a string hash table.  */
  104.  
  105. static struct bfd_hash_entry *
  106. string_hash_newfunc (entry, table, string)
  107.      struct bfd_hash_entry *entry;
  108.      struct bfd_hash_table *table;
  109.      const char *string;
  110. {
  111.   struct string_hash_entry *ret = (struct string_hash_entry *) entry;
  112.  
  113.   /* Allocate the structure if it has not already been allocated by a
  114.      subclass.  */
  115.   if (ret == (struct string_hash_entry *) NULL)
  116.     ret = ((struct string_hash_entry *)
  117.        bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
  118.   if (ret == (struct string_hash_entry *) NULL)
  119.     {
  120.       bfd_set_error (bfd_error_no_memory);
  121.       return NULL;
  122.     }
  123.  
  124.   /* Call the allocation method of the superclass.  */
  125.   ret = ((struct string_hash_entry *)
  126.      bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  127.  
  128.   if (ret)
  129.     {
  130.       /* Initialize the local fields.  */
  131.       ret->val = -1;
  132.       ret->next = NULL;
  133.     }
  134.  
  135.   return (struct bfd_hash_entry *) ret;
  136. }
  137.  
  138. /* Look up an entry in an string hash table.  */
  139.  
  140. #define string_hash_lookup(t, string, create, copy) \
  141.   ((struct string_hash_entry *) \
  142.    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
  143.  
  144. /* We can't afford to read in all the debugging information when we do
  145.    a link.  Instead, we build a list of these structures to show how
  146.    different parts of the input file map to the output file.  */
  147.  
  148. struct shuffle
  149. {
  150.   /* The next entry in this linked list.  */
  151.   struct shuffle *next;
  152.   /* The length of the information.  */
  153.   unsigned long size;
  154.   /* Whether this information comes from a file or not.  */
  155.   boolean filep;
  156.   union
  157.     {
  158.       struct
  159.     {
  160.       /* The BFD the data comes from.  */
  161.       bfd *input_bfd;
  162.       /* The offset within input_bfd.  */
  163.       file_ptr offset;
  164.     } file;
  165.       /* The data to be written out.  */
  166.       PTR memory;
  167.     } u;
  168. };
  169.  
  170. /* This structure holds information across calls to
  171.    bfd_ecoff_debug_accumulate.  */
  172.  
  173. struct accumulate
  174. {
  175.   /* The FDR hash table.  */
  176.   struct string_hash_table fdr_hash;
  177.   /* The strings hash table.  */
  178.   struct string_hash_table str_hash;
  179.   /* Linked lists describing how to shuffle the input debug
  180.      information into the output file.  We keep a pointer to both the
  181.      head and the tail.  */
  182.   struct shuffle *line;
  183.   struct shuffle *line_end;
  184.   struct shuffle *pdr;
  185.   struct shuffle *pdr_end;
  186.   struct shuffle *sym;
  187.   struct shuffle *sym_end;
  188.   struct shuffle *opt;
  189.   struct shuffle *opt_end;
  190.   struct shuffle *aux;
  191.   struct shuffle *aux_end;
  192.   struct shuffle *ss;
  193.   struct shuffle *ss_end;
  194.   struct string_hash_entry *ss_hash;
  195.   struct string_hash_entry *ss_hash_end;
  196.   struct shuffle *fdr;
  197.   struct shuffle *fdr_end;
  198.   struct shuffle *rfd;
  199.   struct shuffle *rfd_end;
  200.   /* The size of the largest file shuffle.  */
  201.   unsigned long largest_file_shuffle;
  202.   /* An obstack for debugging information.  */
  203.   struct obstack memory;
  204. };
  205.  
  206. /* Add a file entry to a shuffle list.  */
  207.  
  208. static boolean add_file_shuffle PARAMS ((struct accumulate *,
  209.                       struct shuffle **,
  210.                       struct shuffle **, bfd *, file_ptr,
  211.                       unsigned long));
  212.  
  213. static boolean
  214. add_file_shuffle (ainfo, head, tail, input_bfd, offset, size)
  215.      struct accumulate *ainfo;
  216.      struct shuffle **head;
  217.      struct shuffle **tail;
  218.      bfd *input_bfd;
  219.      file_ptr offset;
  220.      unsigned long size;
  221. {
  222.   struct shuffle *n;
  223.  
  224.   if (*tail != (struct shuffle *) NULL
  225.       && (*tail)->filep
  226.       && (*tail)->u.file.input_bfd == input_bfd
  227.       && (*tail)->u.file.offset + (*tail)->size == offset)
  228.     {
  229.       /* Just merge this entry onto the existing one.  */
  230.       (*tail)->size += size;
  231.       if ((*tail)->size > ainfo->largest_file_shuffle)
  232.     ainfo->largest_file_shuffle = (*tail)->size;
  233.       return true;
  234.     }
  235.  
  236.   n = (struct shuffle *) obstack_alloc (&ainfo->memory,
  237.                     sizeof (struct shuffle));
  238.   if (!n)
  239.     {
  240.       bfd_set_error (bfd_error_no_memory);
  241.       return false;
  242.     }
  243.   n->next = NULL;
  244.   n->size = size;
  245.   n->filep = true;
  246.   n->u.file.input_bfd = input_bfd;
  247.   n->u.file.offset = offset;
  248.   if (*head == (struct shuffle *) NULL)
  249.     *head = n;
  250.   if (*tail != (struct shuffle *) NULL)
  251.     (*tail)->next = n;
  252.   *tail = n;
  253.   if (size > ainfo->largest_file_shuffle)
  254.     ainfo->largest_file_shuffle = size;
  255.   return true;
  256. }
  257.  
  258. /* Add a memory entry to a shuffle list.  */
  259.  
  260. static boolean add_memory_shuffle PARAMS ((struct accumulate *,
  261.                        struct shuffle **head,
  262.                        struct shuffle **tail,
  263.                        bfd_byte *data, unsigned long size));
  264.  
  265. static boolean
  266. add_memory_shuffle (ainfo, head, tail, data, size)
  267.      struct accumulate *ainfo;
  268.      struct shuffle **head;
  269.      struct shuffle **tail;
  270.      bfd_byte *data;
  271.      unsigned long size;
  272. {
  273.   struct shuffle *n;
  274.      
  275.   n = (struct shuffle *) obstack_alloc (&ainfo->memory,
  276.                     sizeof (struct shuffle));
  277.   if (!n)
  278.     {
  279.       bfd_set_error (bfd_error_no_memory);
  280.       return false;
  281.     }
  282.   n->next = NULL;
  283.   n->size = size;
  284.   n->filep = false;
  285.   n->u.memory = (PTR) data;
  286.   if (*head == (struct shuffle *) NULL)
  287.     *head = n;
  288.   if (*tail != (struct shuffle *) NULL)
  289.     (*tail)->next = n;
  290.   *tail = n;
  291.   return true;
  292. }
  293.  
  294. /* Initialize the FDR hash table.  This returns a handle which is then
  295.    passed in to bfd_ecoff_debug_accumulate, et. al.  */
  296.  
  297. /*ARGSUSED*/
  298. PTR
  299. bfd_ecoff_debug_init (output_bfd, output_debug, output_swap, info)
  300.      bfd *output_bfd;
  301.      struct ecoff_debug_info *output_debug;
  302.      const struct ecoff_debug_swap *output_swap;
  303.      struct bfd_link_info *info;
  304. {
  305.   struct accumulate *ainfo;
  306.  
  307.   ainfo = (struct accumulate *) malloc (sizeof (struct accumulate));
  308.   if (!ainfo)
  309.     {
  310.       bfd_set_error (bfd_error_no_memory);
  311.       return NULL;
  312.     }
  313.   if (! bfd_hash_table_init_n (&ainfo->fdr_hash.table, string_hash_newfunc,
  314.                    1021))
  315.     return NULL;
  316.  
  317.   ainfo->line = NULL;
  318.   ainfo->line_end = NULL;
  319.   ainfo->pdr = NULL;
  320.   ainfo->pdr_end = NULL;
  321.   ainfo->sym = NULL;
  322.   ainfo->sym_end = NULL;
  323.   ainfo->opt = NULL;
  324.   ainfo->opt_end = NULL;
  325.   ainfo->aux = NULL;
  326.   ainfo->aux_end = NULL;
  327.   ainfo->ss = NULL;
  328.   ainfo->ss_end = NULL;
  329.   ainfo->ss_hash = NULL;
  330.   ainfo->ss_hash_end = NULL;
  331.   ainfo->fdr = NULL;
  332.   ainfo->fdr_end = NULL;
  333.   ainfo->rfd = NULL;
  334.   ainfo->rfd_end = NULL;
  335.  
  336.   ainfo->largest_file_shuffle = 0;
  337.  
  338.   if (! info->relocateable)
  339.     {
  340.       if (! bfd_hash_table_init (&ainfo->str_hash.table, string_hash_newfunc))
  341.     return NULL;
  342.  
  343.       /* The first entry in the string table is the empty string.  */
  344.       output_debug->symbolic_header.issMax = 1;
  345.     }
  346.  
  347.   if (!obstack_begin (&ainfo->memory, 4050))
  348.     {
  349.       bfd_set_error (bfd_error_no_memory);
  350.       return NULL;
  351.     }
  352.  
  353.   return (PTR) ainfo;
  354. }
  355.  
  356. /* Free the accumulated debugging information.  */
  357.  
  358. /*ARGSUSED*/
  359. void
  360. bfd_ecoff_debug_free (handle, output_bfd, output_debug, output_swap, info)
  361.      PTR handle;
  362.      bfd *output_bfd;
  363.      struct ecoff_debug_info *output_debug;
  364.      const struct ecoff_debug_swap *output_swap;
  365.      struct bfd_link_info *info;
  366. {
  367.   struct accumulate *ainfo = (struct accumulate *) handle;
  368.   
  369.   bfd_hash_table_free (&ainfo->fdr_hash.table);
  370.  
  371.   if (! info->relocateable)
  372.     bfd_hash_table_free (&ainfo->str_hash.table);
  373.  
  374.   obstack_free (&ainfo->memory, (PTR) NULL);
  375.  
  376.   free (ainfo);
  377. }
  378.  
  379. /* Accumulate the debugging information from INPUT_BFD into
  380.    OUTPUT_BFD.  The INPUT_DEBUG argument points to some ECOFF
  381.    debugging information which we want to link into the information
  382.    pointed to by the OUTPUT_DEBUG argument.  OUTPUT_SWAP and
  383.    INPUT_SWAP point to the swapping information needed.  INFO is the
  384.    linker information structure.  HANDLE is returned by
  385.    bfd_ecoff_debug_init.  */
  386.  
  387. /*ARGSUSED*/
  388. boolean
  389. bfd_ecoff_debug_accumulate (handle, output_bfd, output_debug, output_swap,
  390.                 input_bfd, input_debug, input_swap,
  391.                 info)
  392.      PTR handle;
  393.      bfd *output_bfd;
  394.      struct ecoff_debug_info *output_debug;
  395.      const struct ecoff_debug_swap *output_swap;
  396.      bfd *input_bfd;
  397.      struct ecoff_debug_info *input_debug;
  398.      const struct ecoff_debug_swap *input_swap;
  399.      struct bfd_link_info *info;
  400. {
  401.   struct accumulate *ainfo = (struct accumulate *) handle;
  402.   void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
  403.     = input_swap->swap_sym_in;
  404.   void (* const swap_rfd_in) PARAMS ((bfd *, PTR, RFDT *))
  405.     = input_swap->swap_rfd_in;
  406.   void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
  407.     = output_swap->swap_sym_out;
  408.   void (* const swap_fdr_out) PARAMS ((bfd *, const FDR *, PTR))
  409.     = output_swap->swap_fdr_out;
  410.   void (* const swap_rfd_out) PARAMS ((bfd *, const RFDT *, PTR))
  411.     = output_swap->swap_rfd_out;
  412.   bfd_size_type external_pdr_size = output_swap->external_pdr_size;
  413.   bfd_size_type external_sym_size = output_swap->external_sym_size;
  414.   bfd_size_type external_opt_size = output_swap->external_opt_size;
  415.   bfd_size_type external_fdr_size = output_swap->external_fdr_size;
  416.   bfd_size_type external_rfd_size = output_swap->external_rfd_size;
  417.   HDRR * const output_symhdr = &output_debug->symbolic_header;
  418.   HDRR * const input_symhdr = &input_debug->symbolic_header;
  419.   bfd_vma section_adjust[scMax];
  420.   asection *sec;
  421.   bfd_byte *fdr_start;
  422.   bfd_byte *fdr_ptr;
  423.   bfd_byte *fdr_end;
  424.   bfd_size_type fdr_add;
  425.   unsigned int copied;
  426.   RFDT i;
  427.   unsigned long sz;
  428.   bfd_byte *rfd_out;
  429.   bfd_byte *rfd_in;
  430.   bfd_byte *rfd_end;
  431.   long newrfdbase = 0;
  432.   long oldrfdbase = 0;
  433.   bfd_byte *fdr_out;
  434.  
  435.   /* Use section_adjust to hold the value to add to a symbol in a
  436.      particular section.  */
  437.   memset ((PTR) section_adjust, 0, sizeof section_adjust);
  438.  
  439. #define SET(name, indx) \
  440.   sec = bfd_get_section_by_name (input_bfd, name); \
  441.   if (sec != NULL) \
  442.     section_adjust[indx] = (sec->output_section->vma \
  443.                 + sec->output_offset \
  444.                 - sec->vma);
  445.  
  446.   SET (".text", scText);
  447.   SET (".data", scData);
  448.   SET (".bss", scBss);
  449.   SET (".sdata", scSData);
  450.   SET (".sbss", scSBss);
  451.   /* scRdata section may be either .rdata or .rodata.  */
  452.   SET (".rdata", scRData);
  453.   SET (".rodata", scRData);
  454.   SET (".init", scInit);
  455.   SET (".fini", scFini);
  456.  
  457. #undef SET
  458.  
  459.   /* Find all the debugging information based on the FDR's.  We need
  460.      to handle them whether they are swapped or not.  */
  461.   if (input_debug->fdr != (FDR *) NULL)
  462.     {
  463.       fdr_start = (bfd_byte *) input_debug->fdr;
  464.       fdr_add = sizeof (FDR);
  465.     }
  466.   else
  467.     {
  468.       fdr_start = (bfd_byte *) input_debug->external_fdr;
  469.       fdr_add = input_swap->external_fdr_size;
  470.     }
  471.   fdr_end = fdr_start + input_symhdr->ifdMax * fdr_add;
  472.  
  473.   input_debug->ifdmap = (RFDT *) bfd_alloc (input_bfd,
  474.                         (input_symhdr->ifdMax
  475.                          * sizeof (RFDT)));
  476.  
  477.   sz = (input_symhdr->crfd + input_symhdr->ifdMax) * external_rfd_size;
  478.   rfd_out = (bfd_byte *) obstack_alloc (&ainfo->memory, sz);
  479.   if (!input_debug->ifdmap || !rfd_out)
  480.     {
  481.       bfd_set_error (bfd_error_no_memory);
  482.       return false;
  483.     }
  484.   if (!add_memory_shuffle (ainfo, &ainfo->rfd, &ainfo->rfd_end, rfd_out, sz))
  485.     return false;
  486.  
  487.   copied = 0;
  488.  
  489.   /* Look through the FDR's to see which ones we are going to include
  490.      in the final output.  We do not want duplicate FDR information
  491.      for header files, because ECOFF debugging is often very large.
  492.      When we find an FDR with no line information which can be merged,
  493.      we look it up in a hash table to ensure that we only include it
  494.      once.  We keep a table mapping FDR numbers to the final number
  495.      they get with the BFD, so that we can refer to it when we write
  496.      out the external symbols.  */
  497.   for (fdr_ptr = fdr_start, i = 0;
  498.        fdr_ptr < fdr_end;
  499.        fdr_ptr += fdr_add, i++, rfd_out += external_rfd_size)
  500.     {
  501.       FDR fdr;
  502.  
  503.       if (input_debug->fdr != (FDR *) NULL)
  504.     fdr = *(FDR *) fdr_ptr;
  505.       else
  506.     (*input_swap->swap_fdr_in) (input_bfd, (PTR) fdr_ptr, &fdr);
  507.  
  508.       /* See if this FDR can be merged with an existing one.  */
  509.       if (fdr.cbLine == 0 && fdr.rss != -1 && fdr.fMerge)
  510.     {
  511.       const char *name;
  512.       char *lookup;
  513.       struct string_hash_entry *fh;
  514.  
  515.       /* We look up a string formed from the file name and the
  516.          number of symbols.  Sometimes an include file will
  517.          conditionally define a typedef or something based on the
  518.          order of include files.  Using the number of symbols as a
  519.          hash reduces the chance that we will merge symbol
  520.          information that should not be merged.  */
  521.       name = input_debug->ss + fdr.issBase + fdr.rss;
  522.  
  523.       lookup = (char *) malloc (strlen (name) + 20);
  524.       if (lookup == NULL)
  525.         {
  526.           bfd_set_error (bfd_error_no_memory);
  527.           return false;
  528.         }
  529.       sprintf (lookup, "%s %lx", name, fdr.csym);
  530.  
  531.       fh = string_hash_lookup (&ainfo->fdr_hash, lookup, true, true);
  532.       free (lookup);
  533.       if (fh == (struct string_hash_entry *) NULL)
  534.         return false;
  535.  
  536.       if (fh->val != -1)
  537.         {
  538.           input_debug->ifdmap[i] = fh->val;
  539.           (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i,
  540.                    (PTR) rfd_out);
  541.  
  542.           /* Don't copy this FDR.  */
  543.           continue;
  544.         }
  545.  
  546.       fh->val = output_symhdr->ifdMax + copied;
  547.     }
  548.  
  549.       input_debug->ifdmap[i] = output_symhdr->ifdMax + copied;
  550.       (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i, (PTR) rfd_out);
  551.       ++copied;
  552.     }
  553.  
  554.   newrfdbase = output_symhdr->crfd;
  555.   output_symhdr->crfd += input_symhdr->ifdMax;
  556.  
  557.   /* Copy over any existing RFD's.  RFD's are only created by the
  558.      linker, so this will only happen for input files which are the
  559.      result of a partial link.  */
  560.   rfd_in = (bfd_byte *) input_debug->external_rfd;
  561.   rfd_end = rfd_in + input_symhdr->crfd * input_swap->external_rfd_size;
  562.   for (;
  563.        rfd_in < rfd_end;
  564.        rfd_in += input_swap->external_rfd_size)
  565.     {
  566.       RFDT rfd;
  567.  
  568.       (*swap_rfd_in) (input_bfd, (PTR) rfd_in, &rfd);
  569.       BFD_ASSERT (rfd >= 0 && rfd < input_symhdr->ifdMax);
  570.       rfd = input_debug->ifdmap[rfd];
  571.       (*swap_rfd_out) (output_bfd, &rfd, (PTR) rfd_out);
  572.       rfd_out += external_rfd_size;
  573.     }
  574.  
  575.   oldrfdbase = output_symhdr->crfd;
  576.   output_symhdr->crfd += input_symhdr->crfd;
  577.  
  578.   /* Look through the FDR's and copy over all associated debugging
  579.      information.  */
  580.   sz = copied * external_fdr_size;
  581.   fdr_out = (bfd_byte *) obstack_alloc (&ainfo->memory, sz);
  582.   if (!fdr_out)
  583.     {
  584.       bfd_set_error (bfd_error_no_memory);
  585.       return false;
  586.     }
  587.   if (!add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end, fdr_out, sz))
  588.     return false;
  589.   for (fdr_ptr = fdr_start, i = 0;
  590.        fdr_ptr < fdr_end;
  591.        fdr_ptr += fdr_add, i++)
  592.     {
  593.       FDR fdr;
  594.       bfd_vma fdr_adr;
  595.       bfd_byte *sym_out;
  596.       bfd_byte *lraw_src;
  597.       bfd_byte *lraw_end;
  598.       boolean fgotfilename;
  599.  
  600.       if (input_debug->ifdmap[i] < output_symhdr->ifdMax)
  601.     {
  602.       /* We are not copying this FDR.  */
  603.       continue;
  604.     }
  605.  
  606.       if (input_debug->fdr != (FDR *) NULL)
  607.     fdr = *(FDR *) fdr_ptr;
  608.       else
  609.     (*input_swap->swap_fdr_in) (input_bfd, (PTR) fdr_ptr, &fdr);
  610.  
  611.       fdr_adr = fdr.adr;
  612.  
  613.       /* Adjust the FDR address for any changes that may have been
  614.      made by relaxing.  */
  615.       if (input_debug->adjust != (struct ecoff_value_adjust *) NULL)
  616.     {
  617.       struct ecoff_value_adjust *adjust;
  618.  
  619.       for (adjust = input_debug->adjust;
  620.            adjust != (struct ecoff_value_adjust *) NULL;
  621.            adjust = adjust->next)
  622.         if (fdr_adr >= adjust->start
  623.         && fdr_adr < adjust->end)
  624.           fdr.adr += adjust->adjust;
  625.     }
  626.  
  627.       /* FIXME: It is conceivable that this FDR points to the .init or
  628.      .fini section, in which case this will not do the right
  629.      thing.  */
  630.       fdr.adr += section_adjust[scText];
  631.  
  632.       /* Swap in the local symbols, adjust their values, and swap them
  633.      out again.  */
  634.       fgotfilename = false;
  635.       sz = fdr.csym * external_sym_size;
  636.       sym_out = (bfd_byte *) obstack_alloc (&ainfo->memory, sz);
  637.       if (!sym_out)
  638.     {
  639.       bfd_set_error (bfd_error_no_memory);
  640.       return false;
  641.     }
  642.       if (!add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end, sym_out,
  643.                    sz))
  644.     return false;
  645.       lraw_src = ((bfd_byte *) input_debug->external_sym
  646.           + fdr.isymBase * input_swap->external_sym_size);
  647.       lraw_end = lraw_src + fdr.csym * input_swap->external_sym_size;
  648.       for (;  lraw_src < lraw_end;  lraw_src += input_swap->external_sym_size)
  649.     {
  650.       SYMR internal_sym;
  651.  
  652.       (*swap_sym_in) (input_bfd, (PTR) lraw_src, &internal_sym);
  653.  
  654.       BFD_ASSERT (internal_sym.sc != scCommon
  655.               && internal_sym.sc != scSCommon);
  656.  
  657.       /* Adjust the symbol value if appropriate.  */
  658.       switch (internal_sym.st)
  659.         {
  660.         case stNil:
  661.           if (ECOFF_IS_STAB (&internal_sym))
  662.         break;
  663.           /* Fall through.  */
  664.         case stGlobal:
  665.         case stStatic:
  666.         case stLabel:
  667.         case stProc:
  668.         case stStaticProc:
  669.           if (input_debug->adjust != (struct ecoff_value_adjust *) NULL)
  670.         {
  671.           bfd_vma value;
  672.           struct ecoff_value_adjust *adjust;
  673.  
  674.           value = internal_sym.value;
  675.           for (adjust = input_debug->adjust;
  676.                adjust != (struct ecoff_value_adjust *) NULL;
  677.                adjust = adjust->next)
  678.             if (value >= adjust->start
  679.             && value < adjust->end)
  680.               internal_sym.value += adjust->adjust;
  681.         }
  682.           internal_sym.value += section_adjust[internal_sym.sc];
  683.           break;
  684.  
  685.         default:
  686.           break;
  687.         }
  688.  
  689.       /* If we are doing a final link, we hash all the strings in
  690.          the local symbol table together.  This reduces the amount
  691.          of space required by debugging information.  We don't do
  692.          this when performing a relocateable link because it would
  693.          prevent us from easily merging different FDR's.  */
  694.       if (! info->relocateable)
  695.         {
  696.           boolean ffilename;
  697.           const char *name;
  698.  
  699.           if (! fgotfilename && internal_sym.iss == fdr.rss)
  700.         ffilename = true;
  701.           else
  702.         ffilename = false;
  703.  
  704.           /* Hash the name into the string table.  */
  705.           name = input_debug->ss + fdr.issBase + internal_sym.iss;
  706.           if (*name == '\0')
  707.         internal_sym.iss = 0;
  708.           else
  709.         {
  710.           struct string_hash_entry *sh;
  711.  
  712.           sh = string_hash_lookup (&ainfo->str_hash, name, true, true);
  713.           if (sh == (struct string_hash_entry *) NULL)
  714.             return false;
  715.           if (sh->val == -1)
  716.             {
  717.               sh->val = output_symhdr->issMax;
  718.               output_symhdr->issMax += strlen (name) + 1;
  719.               if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
  720.             ainfo->ss_hash = sh;
  721.               if (ainfo->ss_hash_end
  722.               != (struct string_hash_entry *) NULL)
  723.             ainfo->ss_hash_end->next = sh;
  724.               ainfo->ss_hash_end = sh;
  725.             }
  726.           internal_sym.iss = sh->val;
  727.         }
  728.  
  729.           if (ffilename)
  730.         {
  731.           fdr.rss = internal_sym.iss;
  732.           fgotfilename = true;
  733.         }
  734.         }
  735.  
  736.       (*swap_sym_out) (output_bfd, &internal_sym, sym_out);
  737.       sym_out += external_sym_size;
  738.     }
  739.  
  740.       fdr.isymBase = output_symhdr->isymMax;
  741.       output_symhdr->isymMax += fdr.csym;
  742.  
  743.       /* Copy the information that does not need swapping.  */
  744.  
  745.       /* FIXME: If we are relaxing, we need to adjust the line
  746.      numbers.  Frankly, forget it.  Anybody using stabs debugging
  747.      information will not use this line number information, and
  748.      stabs are adjusted correctly.  */
  749.       if (fdr.cbLine > 0)
  750.     {
  751.       if (!add_file_shuffle (ainfo, &ainfo->line, &ainfo->line_end,
  752.                  input_bfd,
  753.                  input_symhdr->cbLineOffset + fdr.cbLineOffset,
  754.                  fdr.cbLine))
  755.         return false;
  756.       fdr.ilineBase = output_symhdr->ilineMax;
  757.       fdr.cbLineOffset = output_symhdr->cbLine;
  758.       output_symhdr->ilineMax += fdr.cline;
  759.       output_symhdr->cbLine += fdr.cbLine;
  760.     }
  761.       if (fdr.caux > 0)
  762.     {
  763.       if (!add_file_shuffle (ainfo, &ainfo->aux, &ainfo->aux_end,
  764.                  input_bfd,
  765.                  (input_symhdr->cbAuxOffset
  766.                   + fdr.iauxBase * sizeof (union aux_ext)),
  767.                  fdr.caux * sizeof (union aux_ext)))
  768.         return false;
  769.       fdr.iauxBase = output_symhdr->iauxMax;
  770.       output_symhdr->iauxMax += fdr.caux;
  771.     }
  772.       if (! info->relocateable)
  773.     {
  774.  
  775.       /* When are are hashing strings, we lie about the number of
  776.          strings attached to each FDR.  We need to set cbSs
  777.          because some versions of dbx apparently use it to decide
  778.          how much of the string table to read in.  */
  779.       fdr.issBase = 0;
  780.       fdr.cbSs = output_symhdr->issMax;
  781.     }
  782.       else if (fdr.cbSs > 0)
  783.     {
  784.       if (!add_file_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end,
  785.                  input_bfd,
  786.                  input_symhdr->cbSsOffset + fdr.issBase,
  787.                  fdr.cbSs))
  788.         return false;
  789.       fdr.issBase = output_symhdr->issMax;
  790.       output_symhdr->issMax += fdr.cbSs;
  791.     }
  792.  
  793.       if ((output_bfd->xvec->header_byteorder_big_p
  794.        == input_bfd->xvec->header_byteorder_big_p)
  795.       && input_debug->adjust == (struct ecoff_value_adjust *) NULL)
  796.     {
  797.       /* The two BFD's have the same endianness, and we don't have
  798.          to adjust the PDR addresses, so simply copying the
  799.          information will suffice.  */
  800.       BFD_ASSERT (external_pdr_size == input_swap->external_pdr_size);
  801.       if (fdr.cpd > 0)
  802.         {
  803.           if (!add_file_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end,
  804.                      input_bfd,
  805.                      (input_symhdr->cbPdOffset
  806.                       + fdr.ipdFirst * external_pdr_size),
  807.                      fdr.cpd * external_pdr_size))
  808.         return false;
  809.         }
  810.       BFD_ASSERT (external_opt_size == input_swap->external_opt_size);
  811.       if (fdr.copt > 0)
  812.         {
  813.           if (!add_file_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end,
  814.                      input_bfd,
  815.                      (input_symhdr->cbOptOffset
  816.                       + fdr.ioptBase * external_opt_size),
  817.                      fdr.copt * external_opt_size))
  818.         return false;
  819.         }
  820.     }
  821.       else
  822.     {
  823.       bfd_size_type outsz, insz;
  824.       bfd_byte *in;
  825.       bfd_byte *end;
  826.       bfd_byte *out;
  827.  
  828.       /* The two BFD's have different endianness, so we must swap
  829.          everything in and out.  This code would always work, but
  830.          it would be unnecessarily slow in the normal case.  */
  831.       outsz = external_pdr_size;
  832.       insz = input_swap->external_pdr_size;
  833.       in = ((bfd_byte *) input_debug->external_pdr
  834.         + fdr.ipdFirst * insz);
  835.       end = in + fdr.cpd * insz;
  836.       sz = fdr.cpd * outsz;
  837.       out = (bfd_byte *) obstack_alloc (&ainfo->memory, sz);
  838.       if (!out)
  839.         {
  840.           bfd_set_error (bfd_error_no_memory);
  841.           return false;
  842.         }
  843.       if (!add_memory_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end, out,
  844.                    sz))
  845.         return false;
  846.       for (; in < end; in += insz, out += outsz)
  847.         {
  848.           PDR pdr;
  849.  
  850.           (*input_swap->swap_pdr_in) (input_bfd, (PTR) in, &pdr);
  851.  
  852.           /* If we have been relaxing, we may have to adjust the
  853.          address.  */
  854.           if (input_debug->adjust != (struct ecoff_value_adjust *) NULL)
  855.         {
  856.           bfd_vma adr;
  857.           struct ecoff_value_adjust *adjust;
  858.  
  859.           adr = fdr_adr + pdr.adr;
  860.           for (adjust = input_debug->adjust;
  861.                adjust != (struct ecoff_value_adjust *) NULL;
  862.                adjust = adjust->next)
  863.             if (adr >= adjust->start
  864.             && adr < adjust->end)
  865.               pdr.adr += adjust->adjust;
  866.         }
  867.  
  868.           (*output_swap->swap_pdr_out) (output_bfd, &pdr, (PTR) out);
  869.         }
  870.  
  871.       /* Swap over the optimization information.  */
  872.       outsz = external_opt_size;
  873.       insz = input_swap->external_opt_size;
  874.       in = ((bfd_byte *) input_debug->external_opt
  875.         + fdr.ioptBase * insz);
  876.       end = in + fdr.copt * insz;
  877.       sz = fdr.copt * outsz;
  878.       out = (bfd_byte *) obstack_alloc (&ainfo->memory, sz);
  879.       if (!out)
  880.         {
  881.           bfd_set_error (bfd_error_no_memory);
  882.           return false;
  883.         }
  884.       if (!add_memory_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end, out,
  885.                    sz))
  886.         return false;
  887.       for (; in < end; in += insz, out += outsz)
  888.         {
  889.           OPTR opt;
  890.  
  891.           (*input_swap->swap_opt_in) (input_bfd, (PTR) in, &opt);
  892.           (*output_swap->swap_opt_out) (output_bfd, &opt, (PTR) out);
  893.         }
  894.     }
  895.  
  896.       fdr.ipdFirst = output_symhdr->ipdMax;
  897.       output_symhdr->ipdMax += fdr.cpd;
  898.       fdr.ioptBase = output_symhdr->ioptMax;
  899.       output_symhdr->ioptMax += fdr.copt;
  900.  
  901.       if (fdr.crfd <= 0)
  902.     {
  903.       /* Point this FDR at the table of RFD's we created.  */
  904.       fdr.rfdBase = newrfdbase;
  905.       fdr.crfd = input_symhdr->ifdMax;
  906.     }
  907.       else
  908.     {
  909.       /* Point this FDR at the remapped RFD's.  */
  910.       fdr.rfdBase += oldrfdbase;
  911.     }
  912.  
  913.       (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
  914.       fdr_out += external_fdr_size;
  915.       ++output_symhdr->ifdMax;
  916.     }
  917.  
  918.   return true;
  919. }
  920.  
  921. /* Add a string to the debugging information we are accumulating.
  922.    Return the offset from the fdr string base.  */
  923.  
  924. static long ecoff_add_string PARAMS ((struct accumulate *,
  925.                       struct bfd_link_info *,
  926.                       struct ecoff_debug_info *,
  927.                       FDR *fdr, const char *string));
  928.  
  929. static long
  930. ecoff_add_string (ainfo, info, debug, fdr, string)
  931.      struct accumulate *ainfo;
  932.      struct bfd_link_info *info;
  933.      struct ecoff_debug_info *debug;
  934.      FDR *fdr;
  935.      const char *string;
  936. {
  937.   HDRR *symhdr;
  938.   size_t len;
  939.   bfd_size_type ret;
  940.  
  941.   symhdr = &debug->symbolic_header;
  942.   len = strlen (string);
  943.   if (info->relocateable)
  944.     {
  945.       if (!add_memory_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end, (PTR) string,
  946.                    len + 1))
  947.     return -1;
  948.       ret = symhdr->issMax;
  949.       symhdr->issMax += len + 1;
  950.       fdr->cbSs += len + 1;
  951.     }
  952.   else
  953.     {
  954.       struct string_hash_entry *sh;
  955.  
  956.       sh = string_hash_lookup (&ainfo->str_hash, string, true, true);
  957.       if (sh == (struct string_hash_entry *) NULL)
  958.     return -1;
  959.       if (sh->val == -1)
  960.     {
  961.       sh->val = symhdr->issMax;
  962.       symhdr->issMax += len + 1;
  963.       if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
  964.         ainfo->ss_hash = sh;
  965.       if (ainfo->ss_hash_end
  966.           != (struct string_hash_entry *) NULL)
  967.         ainfo->ss_hash_end->next = sh;
  968.       ainfo->ss_hash_end = sh;
  969.     }
  970.       ret = sh->val;
  971.     }
  972.  
  973.   return ret;
  974. }
  975.  
  976. /* Add debugging information from a non-ECOFF file.  */
  977.  
  978. boolean
  979. bfd_ecoff_debug_accumulate_other (handle, output_bfd, output_debug,
  980.                   output_swap, input_bfd, info)
  981.      PTR handle;
  982.      bfd *output_bfd;
  983.      struct ecoff_debug_info *output_debug;
  984.      const struct ecoff_debug_swap *output_swap;
  985.      bfd *input_bfd;
  986.      struct bfd_link_info *info;
  987. {
  988.   struct accumulate *ainfo = (struct accumulate *) handle;
  989.   void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
  990.     = output_swap->swap_sym_out;
  991.   HDRR *output_symhdr = &output_debug->symbolic_header;
  992.   FDR fdr;
  993.   asection *sec;
  994.   asymbol **symbols;
  995.   asymbol **sym_ptr;
  996.   asymbol **sym_end;
  997.   long symsize;
  998.   long symcount;
  999.   PTR external_fdr;
  1000.  
  1001.   memset ((PTR) &fdr, 0, sizeof fdr);
  1002.  
  1003.   sec = bfd_get_section_by_name (input_bfd, ".text");
  1004.   if (sec != NULL)
  1005.     fdr.adr = sec->output_section->vma + sec->output_offset;
  1006.   else
  1007.     {
  1008.       /* FIXME: What about .init or .fini?  */
  1009.       fdr.adr = 0;
  1010.     }
  1011.  
  1012.   fdr.issBase = output_symhdr->issMax;
  1013.   fdr.cbSs = 0;
  1014.   fdr.rss = ecoff_add_string (ainfo, info, output_debug, &fdr,
  1015.                   bfd_get_filename (input_bfd));
  1016.   if (fdr.rss == -1)
  1017.     return false;
  1018.   fdr.isymBase = output_symhdr->isymMax;
  1019.  
  1020.   /* Get the local symbols from the input BFD.  */
  1021.   symsize = bfd_get_symtab_upper_bound (input_bfd);
  1022.   if (symsize < 0)
  1023.     return false;
  1024.   symbols = (asymbol **) bfd_alloc (output_bfd, symsize);
  1025.   if (symbols == (asymbol **) NULL)
  1026.     {
  1027.       bfd_set_error (bfd_error_no_memory);
  1028.       return false;
  1029.     }
  1030.   symcount = bfd_canonicalize_symtab (input_bfd, symbols);
  1031.   if (symcount < 0)
  1032.     return false;
  1033.   sym_end = symbols + symcount;
  1034.  
  1035.   /* Handle the local symbols.  Any external symbols are handled
  1036.      separately.  */
  1037.   fdr.csym = 0;
  1038.   for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
  1039.     {
  1040.       SYMR internal_sym;
  1041.       PTR external_sym;
  1042.  
  1043.       if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
  1044.     continue;
  1045.       memset ((PTR) &internal_sym, 0, sizeof internal_sym);
  1046.       internal_sym.iss = ecoff_add_string (ainfo, info, output_debug, &fdr,
  1047.                        (*sym_ptr)->name);
  1048.  
  1049.       if (internal_sym.iss == -1)
  1050.     return false;
  1051.       if (bfd_is_com_section ((*sym_ptr)->section)
  1052.       || bfd_is_und_section ((*sym_ptr)->section))
  1053.     internal_sym.value = (*sym_ptr)->value;
  1054.       else
  1055.     internal_sym.value = ((*sym_ptr)->value
  1056.                   + (*sym_ptr)->section->output_offset
  1057.                   + (*sym_ptr)->section->output_section->vma);
  1058.       internal_sym.st = stNil;
  1059.       internal_sym.sc = scUndefined;
  1060.       internal_sym.index = indexNil;
  1061.  
  1062.       external_sym = (PTR) obstack_alloc (&ainfo->memory,
  1063.                       output_swap->external_sym_size);
  1064.       if (!external_sym)
  1065.     {
  1066.       bfd_set_error (bfd_error_no_memory);
  1067.       return false;
  1068.     }
  1069.       (*swap_sym_out) (output_bfd, &internal_sym, external_sym);
  1070.       add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end,
  1071.               external_sym, output_swap->external_sym_size);
  1072.       ++fdr.csym;
  1073.       ++output_symhdr->isymMax;
  1074.     }
  1075.  
  1076.   bfd_release (output_bfd, (PTR) symbols);
  1077.  
  1078.   /* Leave everything else in the FDR zeroed out.  This will cause
  1079.      the lang field to be langC.  The fBigendian field will
  1080.      indicate little endian format, but it doesn't matter because
  1081.      it only applies to aux fields and there are none.  */
  1082.   external_fdr = (PTR) obstack_alloc (&ainfo->memory,
  1083.                       output_swap->external_fdr_size);
  1084.   if (!external_fdr)
  1085.     {
  1086.       bfd_set_error (bfd_error_no_memory);
  1087.       return false;
  1088.     }
  1089.   (*output_swap->swap_fdr_out) (output_bfd, &fdr, external_fdr);
  1090.   add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end,
  1091.               external_fdr, output_swap->external_fdr_size);
  1092.  
  1093.   ++output_symhdr->ifdMax;
  1094.  
  1095.   return true;
  1096. }
  1097.  
  1098. /* Set up ECOFF debugging information for the external symbols.
  1099.    FIXME: This is done using a memory buffer, but it should be
  1100.    probably be changed to use a shuffle structure.  The assembler uses
  1101.    this interface, so that must be changed to do something else.  */
  1102.  
  1103. boolean
  1104. bfd_ecoff_debug_externals (abfd, debug, swap, relocateable, get_extr,
  1105.                set_index)
  1106.      bfd *abfd;
  1107.      struct ecoff_debug_info *debug;
  1108.      const struct ecoff_debug_swap *swap;
  1109.      boolean relocateable;
  1110.      boolean (*get_extr) PARAMS ((asymbol *, EXTR *));
  1111.      void (*set_index) PARAMS ((asymbol *, bfd_size_type));
  1112. {
  1113.   HDRR * const symhdr = &debug->symbolic_header;
  1114.   asymbol **sym_ptr_ptr;
  1115.   size_t c;
  1116.  
  1117.   sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1118.   if (sym_ptr_ptr == NULL)
  1119.     return true;
  1120.  
  1121.   for (c = bfd_get_symcount (abfd); c > 0; c--, sym_ptr_ptr++)
  1122.     {
  1123.       asymbol *sym_ptr;
  1124.       EXTR esym;
  1125.  
  1126.       sym_ptr = *sym_ptr_ptr;
  1127.  
  1128.       /* Get the external symbol information.  */
  1129.       if ((*get_extr) (sym_ptr, &esym) == false)
  1130.     continue;
  1131.  
  1132.       /* If we're producing an executable, move common symbols into
  1133.      bss.  */
  1134.       if (relocateable == false)
  1135.     {
  1136.       if (esym.asym.sc == scCommon)
  1137.         esym.asym.sc = scBss;
  1138.       else if (esym.asym.sc == scSCommon)
  1139.         esym.asym.sc = scSBss;
  1140.     }
  1141.  
  1142.       if (bfd_is_com_section (sym_ptr->section)
  1143.       || bfd_is_und_section (sym_ptr->section)
  1144.       || sym_ptr->section->output_section == (asection *) NULL)
  1145.     {
  1146.       /* FIXME: gas does not keep the value of a small undefined
  1147.          symbol in the symbol itself, because of relocation
  1148.          problems.  */
  1149.       if (esym.asym.sc != scSUndefined
  1150.           || esym.asym.value == 0
  1151.           || sym_ptr->value != 0)
  1152.         esym.asym.value = sym_ptr->value;
  1153.     }
  1154.       else
  1155.     esym.asym.value = (sym_ptr->value
  1156.                + sym_ptr->section->output_offset
  1157.                + sym_ptr->section->output_section->vma);
  1158.  
  1159.       if (set_index)
  1160.     (*set_index) (sym_ptr, (bfd_size_type) symhdr->iextMax);
  1161.  
  1162.       if (! bfd_ecoff_debug_one_external (abfd, debug, swap,
  1163.                       sym_ptr->name, &esym))
  1164.     return false;
  1165.     }
  1166.  
  1167.   return true;
  1168. }
  1169.  
  1170. /* Add a single external symbol to the debugging information.  */
  1171.  
  1172. boolean
  1173. bfd_ecoff_debug_one_external (abfd, debug, swap, name, esym)
  1174.      bfd *abfd;
  1175.      struct ecoff_debug_info *debug;
  1176.      const struct ecoff_debug_swap *swap;
  1177.      const char *name;
  1178.      EXTR *esym;
  1179. {
  1180.   const bfd_size_type external_ext_size = swap->external_ext_size;
  1181.   void (* const swap_ext_out) PARAMS ((bfd *, const EXTR *, PTR))
  1182.     = swap->swap_ext_out;
  1183.   HDRR * const symhdr = &debug->symbolic_header;
  1184.   size_t namelen;
  1185.  
  1186.   namelen = strlen (name);
  1187.  
  1188.   if (debug->ssext_end - debug->ssext
  1189.       < symhdr->issExtMax + namelen + 1)
  1190.     {
  1191.       if (ecoff_add_bytes ((char **) &debug->ssext,
  1192.                (char **) &debug->ssext_end,
  1193.                symhdr->issExtMax + namelen + 1)
  1194.       == false)
  1195.     return false;
  1196.     }
  1197.   if ((char *) debug->external_ext_end - (char *) debug->external_ext
  1198.       < (symhdr->iextMax + 1) * external_ext_size)
  1199.     {
  1200.       if (ecoff_add_bytes ((char **) &debug->external_ext,
  1201.                (char **) &debug->external_ext_end,
  1202.                (symhdr->iextMax + 1) * external_ext_size)
  1203.       == false)
  1204.     return false;
  1205.     }
  1206.  
  1207.   esym->asym.iss = symhdr->issExtMax;
  1208.  
  1209.   (*swap_ext_out) (abfd, esym,
  1210.            ((char *) debug->external_ext
  1211.             + symhdr->iextMax * swap->external_ext_size));
  1212.  
  1213.   ++symhdr->iextMax;
  1214.  
  1215.   strcpy (debug->ssext + symhdr->issExtMax, name);
  1216.   symhdr->issExtMax += namelen + 1;
  1217.  
  1218.   return true;
  1219. }
  1220.  
  1221. /* Align the ECOFF debugging information.  */
  1222.  
  1223. /*ARGSUSED*/
  1224. static void
  1225. ecoff_align_debug (abfd, debug, swap)
  1226.      bfd *abfd;
  1227.      struct ecoff_debug_info *debug;
  1228.      const struct ecoff_debug_swap *swap;
  1229. {
  1230.   HDRR * const symhdr = &debug->symbolic_header;
  1231.   bfd_size_type debug_align, aux_align, rfd_align;
  1232.   size_t add;
  1233.  
  1234.   /* Adjust the counts so that structures are aligned.  */
  1235.   debug_align = swap->debug_align;
  1236.   aux_align = debug_align / sizeof (union aux_ext);
  1237.   rfd_align = debug_align / swap->external_rfd_size;
  1238.  
  1239.   add = debug_align - (symhdr->cbLine & (debug_align - 1));
  1240.   if (add != debug_align)
  1241.     {
  1242.       if (debug->line != (unsigned char *) NULL)
  1243.     memset ((PTR) (debug->line + symhdr->cbLine), 0, add);
  1244.       symhdr->cbLine += add;
  1245.     }
  1246.  
  1247.   add = debug_align - (symhdr->issMax & (debug_align - 1));
  1248.   if (add != debug_align)
  1249.     {
  1250.       if (debug->ss != (char *) NULL)
  1251.     memset ((PTR) (debug->ss + symhdr->issMax), 0, add);
  1252.       symhdr->issMax += add;
  1253.     }
  1254.  
  1255.   add = debug_align - (symhdr->issExtMax & (debug_align - 1));
  1256.   if (add != debug_align)
  1257.     {
  1258.       if (debug->ssext != (char *) NULL)
  1259.     memset ((PTR) (debug->ssext + symhdr->issExtMax), 0, add);
  1260.       symhdr->issExtMax += add;
  1261.     }
  1262.  
  1263.   add = aux_align - (symhdr->iauxMax & (aux_align - 1));
  1264.   if (add != aux_align)
  1265.     {
  1266.       if (debug->external_aux != (union aux_ext *) NULL)
  1267.     memset ((PTR) (debug->external_aux + symhdr->iauxMax), 0,
  1268.         add * sizeof (union aux_ext));
  1269.       symhdr->iauxMax += add;
  1270.     }
  1271.  
  1272.   add = rfd_align - (symhdr->crfd & (rfd_align - 1));
  1273.   if (add != rfd_align)
  1274.     {
  1275.       if (debug->external_rfd != (PTR) NULL)
  1276.     memset ((PTR) ((char *) debug->external_rfd
  1277.                + symhdr->crfd * swap->external_rfd_size),
  1278.         0, add * swap->external_rfd_size);
  1279.       symhdr->crfd += add;
  1280.     }
  1281. }
  1282.  
  1283. /* Return the size required by the ECOFF debugging information.  */
  1284.  
  1285. bfd_size_type
  1286. bfd_ecoff_debug_size (abfd, debug, swap)
  1287.      bfd *abfd;
  1288.      struct ecoff_debug_info *debug;
  1289.      const struct ecoff_debug_swap *swap;
  1290. {
  1291.   bfd_size_type tot;
  1292.  
  1293.   ecoff_align_debug (abfd, debug, swap);
  1294.   tot = swap->external_hdr_size;
  1295.  
  1296. #define ADD(count, size) \
  1297.   tot += debug->symbolic_header.count * size
  1298.  
  1299.   ADD (cbLine, sizeof (unsigned char));
  1300.   ADD (idnMax, swap->external_dnr_size);
  1301.   ADD (ipdMax, swap->external_pdr_size);
  1302.   ADD (isymMax, swap->external_sym_size);
  1303.   ADD (ioptMax, swap->external_opt_size);
  1304.   ADD (iauxMax, sizeof (union aux_ext));
  1305.   ADD (issMax, sizeof (char));
  1306.   ADD (issExtMax, sizeof (char));
  1307.   ADD (ifdMax, swap->external_fdr_size);
  1308.   ADD (crfd, swap->external_rfd_size);
  1309.   ADD (iextMax, swap->external_ext_size);
  1310.  
  1311. #undef ADD
  1312.  
  1313.   return tot;
  1314. }
  1315.  
  1316. /* Write out the ECOFF symbolic header, given the file position it is
  1317.    going to be placed at.  This assumes that the counts are set
  1318.    correctly.  */
  1319.  
  1320. static boolean
  1321. ecoff_write_symhdr (abfd, debug, swap, where)
  1322.      bfd *abfd;
  1323.      struct ecoff_debug_info *debug;
  1324.      const struct ecoff_debug_swap *swap;
  1325.      file_ptr where;
  1326. {
  1327.   HDRR * const symhdr = &debug->symbolic_header;
  1328.   char *buff = NULL;
  1329.  
  1330.   ecoff_align_debug (abfd, debug, swap);
  1331.  
  1332.   /* Go to the right location in the file.  */
  1333.   if (bfd_seek (abfd, where, SEEK_SET) != 0)
  1334.     return false;
  1335.  
  1336.   where += swap->external_hdr_size;
  1337.  
  1338.   symhdr->magic = swap->sym_magic;
  1339.  
  1340.   /* Fill in the file offsets.  */
  1341. #define SET(offset, count, size) \
  1342.   if (symhdr->count == 0) \
  1343.     symhdr->offset = 0; \
  1344.   else \
  1345.     { \
  1346.       symhdr->offset = where; \
  1347.       where += symhdr->count * size; \
  1348.     }
  1349.  
  1350.   SET (cbLineOffset, cbLine, sizeof (unsigned char));
  1351.   SET (cbDnOffset, idnMax, swap->external_dnr_size);
  1352.   SET (cbPdOffset, ipdMax, swap->external_pdr_size);
  1353.   SET (cbSymOffset, isymMax, swap->external_sym_size);
  1354.   SET (cbOptOffset, ioptMax, swap->external_opt_size);
  1355.   SET (cbAuxOffset, iauxMax, sizeof (union aux_ext));
  1356.   SET (cbSsOffset, issMax, sizeof (char));
  1357.   SET (cbSsExtOffset, issExtMax, sizeof (char));
  1358.   SET (cbFdOffset, ifdMax, swap->external_fdr_size);
  1359.   SET (cbRfdOffset, crfd, swap->external_rfd_size);
  1360.   SET (cbExtOffset, iextMax, swap->external_ext_size);
  1361. #undef SET
  1362.  
  1363.   buff = (PTR) malloc (swap->external_hdr_size);
  1364.   if (buff == NULL && swap->external_hdr_size != 0)
  1365.     {
  1366.       bfd_set_error (bfd_error_no_memory);
  1367.       goto error_return;
  1368.     }
  1369.  
  1370.   (*swap->swap_hdr_out) (abfd, symhdr, buff);
  1371.   if (bfd_write (buff, 1, swap->external_hdr_size, abfd)
  1372.       != swap->external_hdr_size)
  1373.     goto error_return;
  1374.  
  1375.   if (buff != NULL)
  1376.     free (buff);
  1377.   return true;
  1378.  error_return:
  1379.   if (buff != NULL)
  1380.     free (buff);
  1381.   return false;
  1382. }
  1383.  
  1384. /* Write out the ECOFF debugging information.  This function assumes
  1385.    that the information (the pointers and counts) in *DEBUG have been
  1386.    set correctly.  WHERE is the position in the file to write the
  1387.    information to.  This function fills in the file offsets in the
  1388.    symbolic header.  */
  1389.  
  1390. boolean
  1391. bfd_ecoff_write_debug (abfd, debug, swap, where)
  1392.      bfd *abfd;
  1393.      struct ecoff_debug_info *debug;
  1394.      const struct ecoff_debug_swap *swap;
  1395.      file_ptr where;
  1396. {
  1397.   HDRR * const symhdr = &debug->symbolic_header;
  1398.  
  1399.   if (! ecoff_write_symhdr (abfd, debug, swap, where))
  1400.     return false;
  1401.  
  1402. #define WRITE(ptr, count, size, offset) \
  1403.   BFD_ASSERT (symhdr->offset == 0 || bfd_tell (abfd) == symhdr->offset); \
  1404.   if (bfd_write ((PTR) debug->ptr, size, symhdr->count, abfd) \
  1405.       != size * symhdr->count) \
  1406.     return false;
  1407.  
  1408.   WRITE (line, cbLine, sizeof (unsigned char), cbLineOffset);
  1409.   WRITE (external_dnr, idnMax, swap->external_dnr_size, cbDnOffset);
  1410.   WRITE (external_pdr, ipdMax, swap->external_pdr_size, cbPdOffset);
  1411.   WRITE (external_sym, isymMax, swap->external_sym_size, cbSymOffset);
  1412.   WRITE (external_opt, ioptMax, swap->external_opt_size, cbOptOffset);
  1413.   WRITE (external_aux, iauxMax, sizeof (union aux_ext), cbAuxOffset);
  1414.   WRITE (ss, issMax, sizeof (char), cbSsOffset);
  1415.   WRITE (ssext, issExtMax, sizeof (char), cbSsExtOffset);
  1416.   WRITE (external_fdr, ifdMax, swap->external_fdr_size, cbFdOffset);
  1417.   WRITE (external_rfd, crfd, swap->external_rfd_size, cbRfdOffset);
  1418.   WRITE (external_ext, iextMax, swap->external_ext_size, cbExtOffset);
  1419. #undef WRITE
  1420.  
  1421.   return true;
  1422. }
  1423.  
  1424. /* Write out a shuffle list.  */
  1425.  
  1426. static boolean ecoff_write_shuffle PARAMS ((bfd *,
  1427.                         const struct ecoff_debug_swap *,
  1428.                         struct shuffle *, PTR space));
  1429.  
  1430. static boolean
  1431. ecoff_write_shuffle (abfd, swap, shuffle, space)
  1432.      bfd *abfd;
  1433.      const struct ecoff_debug_swap *swap;
  1434.      struct shuffle *shuffle;
  1435.      PTR space;
  1436. {
  1437.   register struct shuffle *l;
  1438.   unsigned long total;
  1439.  
  1440.   total = 0;
  1441.   for (l = shuffle; l != (struct shuffle *) NULL; l = l->next)
  1442.     {
  1443.       if (! l->filep)
  1444.     {
  1445.       if (bfd_write (l->u.memory, 1, l->size, abfd) != l->size)
  1446.         return false;
  1447.     }
  1448.       else
  1449.     {
  1450.       if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
  1451.           || bfd_read (space, 1, l->size, l->u.file.input_bfd) != l->size
  1452.           || bfd_write (space, 1, l->size, abfd) != l->size)
  1453.         return false;
  1454.     }
  1455.       total += l->size;
  1456.     }
  1457.  
  1458.   if ((total & (swap->debug_align - 1)) != 0)
  1459.     {
  1460.       int i;
  1461.       bfd_byte *s;
  1462.  
  1463.       i = swap->debug_align - (total & (swap->debug_align - 1));
  1464.       s = (bfd_byte *) malloc (i);
  1465.       if (s == NULL && i != 0)
  1466.     {
  1467.       bfd_set_error (bfd_error_no_memory);
  1468.       return false;
  1469.     }
  1470.  
  1471.       memset ((PTR) s, 0, i);
  1472.       if (bfd_write ((PTR) s, 1, i, abfd) != i)
  1473.     {
  1474.       free (s);
  1475.       return false;
  1476.     }
  1477.       free (s);
  1478.     }
  1479.  
  1480.   return true;
  1481. }
  1482.  
  1483. /* Write out debugging information using accumulated linker
  1484.    information.  */
  1485.  
  1486. boolean
  1487. bfd_ecoff_write_accumulated_debug (handle, abfd, debug, swap, info, where)
  1488.      PTR handle;
  1489.      bfd *abfd;
  1490.      struct ecoff_debug_info *debug;
  1491.      const struct ecoff_debug_swap *swap;
  1492.      struct bfd_link_info *info;
  1493.      file_ptr where;
  1494. {
  1495.   struct accumulate *ainfo = (struct accumulate *) handle;
  1496.   PTR space = NULL;
  1497.  
  1498.   if (! ecoff_write_symhdr (abfd, debug, swap, where))
  1499.     goto error_return;
  1500.  
  1501.   space = (PTR) malloc (ainfo->largest_file_shuffle);
  1502.   if (space == NULL && ainfo->largest_file_shuffle != 0)
  1503.     {
  1504.       bfd_set_error (bfd_error_no_memory);
  1505.       goto error_return;
  1506.     }
  1507.  
  1508.   if (! ecoff_write_shuffle (abfd, swap, ainfo->line, space)
  1509.       || ! ecoff_write_shuffle (abfd, swap, ainfo->pdr, space)
  1510.       || ! ecoff_write_shuffle (abfd, swap, ainfo->sym, space)
  1511.       || ! ecoff_write_shuffle (abfd, swap, ainfo->opt, space)
  1512.       || ! ecoff_write_shuffle (abfd, swap, ainfo->aux, space))
  1513.     goto error_return;
  1514.  
  1515.   /* The string table is written out from the hash table if this is a
  1516.      final link.  */
  1517.   if (info->relocateable)
  1518.     {
  1519.       BFD_ASSERT (ainfo->ss_hash == (struct string_hash_entry *) NULL);
  1520.       if (! ecoff_write_shuffle (abfd, swap, ainfo->ss, space))
  1521.     goto error_return;
  1522.     }
  1523.   else
  1524.     {
  1525.       unsigned long total;
  1526.       bfd_byte null;
  1527.       struct string_hash_entry *sh;
  1528.  
  1529.       BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
  1530.       null = 0;
  1531.       if (bfd_write ((PTR) &null, 1, 1, abfd) != 1)
  1532.     goto error_return;
  1533.       total = 1;
  1534.       BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
  1535.       for (sh = ainfo->ss_hash;
  1536.        sh != (struct string_hash_entry *) NULL;
  1537.        sh = sh->next)
  1538.     {
  1539.       size_t len;
  1540.  
  1541.       len = strlen (sh->root.string);
  1542.       if (bfd_write ((PTR) sh->root.string, 1, len + 1, abfd) != len + 1)
  1543.         goto error_return;
  1544.       total += len + 1;
  1545.     }
  1546.  
  1547.       if ((total & (swap->debug_align - 1)) != 0)
  1548.     {
  1549.       int i;
  1550.       bfd_byte *s;
  1551.  
  1552.       i = swap->debug_align - (total & (swap->debug_align - 1));
  1553.       s = (bfd_byte *) malloc (i);
  1554.       if (s == NULL && i != 0)
  1555.         {
  1556.           bfd_set_error (bfd_error_no_memory);
  1557.           goto error_return;
  1558.         }
  1559.       memset ((PTR) s, 0, i);
  1560.       if (bfd_write ((PTR) s, 1, i, abfd) != i)
  1561.         {
  1562.           free (s);
  1563.           goto error_return;
  1564.         }
  1565.       free (s);
  1566.     }
  1567.     }
  1568.  
  1569.   /* The external strings and symbol are not converted over to using
  1570.      shuffles.  FIXME: They probably should be.  */
  1571.   if (bfd_write (debug->ssext, 1, debug->symbolic_header.issExtMax, abfd)
  1572.       != debug->symbolic_header.issExtMax)
  1573.     goto error_return;
  1574.   if ((debug->symbolic_header.issExtMax & (swap->debug_align - 1)) != 0)
  1575.     {
  1576.       int i;
  1577.       bfd_byte *s;
  1578.  
  1579.       i = (swap->debug_align
  1580.        - (debug->symbolic_header.issExtMax & (swap->debug_align - 1)));
  1581.       s = (bfd_byte *) malloc (i);
  1582.       if (s == NULL && i != 0)
  1583.     {
  1584.       bfd_set_error (bfd_error_no_memory);
  1585.       goto error_return;
  1586.     }
  1587.       memset ((PTR) s, 0, i);
  1588.       if (bfd_write ((PTR) s, 1, i, abfd) != i)
  1589.     {
  1590.       free (s);
  1591.       goto error_return;
  1592.     }
  1593.       free (s);
  1594.     }
  1595.  
  1596.   if (! ecoff_write_shuffle (abfd, swap, ainfo->fdr, space)
  1597.       || ! ecoff_write_shuffle (abfd, swap, ainfo->rfd, space))
  1598.     goto error_return;
  1599.  
  1600.   BFD_ASSERT (debug->symbolic_header.cbExtOffset == 0
  1601.           || debug->symbolic_header.cbExtOffset == bfd_tell (abfd));
  1602.  
  1603.   if (bfd_write (debug->external_ext, swap->external_ext_size,
  1604.          debug->symbolic_header.iextMax, abfd)
  1605.       != debug->symbolic_header.iextMax * swap->external_ext_size)
  1606.     goto error_return;
  1607.  
  1608.   if (space != NULL)
  1609.     free (space);
  1610.   return true;
  1611.  
  1612.  error_return:
  1613.   if (space != NULL)
  1614.     free (space);
  1615.   return false;
  1616. }
  1617.